﻿using CSRedis;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SmartApplication.Api.Applient.Command.Feed;
using SmartApplication.Api.Dto;
using SmartApplication.Api.Ext;
using SmartApplication.ApiCode;
using SmartApplication.Domain.Entity;
using SmartApplication.Infrastructure.Instrument;

namespace SmartApplication.Api.Applient.CommandHandler.Feed
{
    public class ListFeedHandler : IRequestHandler<ListFeedCommand, ApiResult<List<FeedDto>>>
    {
        public ListFeedHandler(IBaseRepository<FeedModel> feedRepository,IBaseRepository<CompaniesModel> companiesRepository,IConfiguration configuration)
        {
            FeedRepository = feedRepository;
            CompaniesRepository = companiesRepository;
            Configuration = configuration;
        }

        public IBaseRepository<FeedModel> FeedRepository { get; }
        public IBaseRepository<CompaniesModel> CompaniesRepository { get; }
        public IConfiguration Configuration { get; }
        public IBaseRepository<HealthModel> HealthRepository { get; }

        /// <summary>
        /// 显示养殖详细信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ApiResult<List<FeedDto>>> Handle(ListFeedCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<List<FeedDto>>();

            var feed = FeedRepository.GetAll();
            var com = CompaniesRepository.GetAll();
            

            var list = from f in feed.Where(x => x.SourceId == request.SourceId)
                       join c in com on f.CompaniesId equals c.Id
                       select new FeedDto()
                       {
                           Id = f.Id,
                           CompaniesId = c.Id,
                           CompaniesName = c.CompaniesName,
                           FeedDays = f.FeedDays,
                           FeedFoodName = f.FeedFoodName,
                           FeedName = f.FeedName,
                           FeedSource = f.FeedSource,
                           FeedVariety = f.FeedVariety,
                           LeaveDate = f.LeaveDate,
                           Pic = f.Pic,
                           SourceId = f.SourceId,
                           AnimalNumber = c.AnimalNumber,
                           ChargeName = c.ChargeName,
                           CompaniesPic = c.Pic
                       };

            res.Code = EnumCode.Success;
            res.Message = "显示养殖详细信息成功";
            res.Data =await list.ToListAsync();


            //var redis = new CSRedisClient(Configuration.GetConnectionString("ConnRedis:Redis"));

            //var csRedis = redis.Get("feedData");
            //if (csRedis == null)
            //{
            //    var fd=res.Data.ToJson();
            //    redis.Set("feedData",fd);
            //    res.Data= await list.ToListAsync();
            //    return res;
            //}
            //else
            //{
            //    res.Data = csRedis.ToObject<List<FeedDto>>();
            //    return res;
            //}
            return res;
        }
    }
}
